package com.nsu.evaluation.controller;

import com.nsu.evaluation.common.ServerResponse;
import com.nsu.evaluation.pojo.Class;
import com.nsu.evaluation.pojo.Course;
import com.nsu.evaluation.pojo.StudentScore;
import com.nsu.evaluation.service.impl.ClassServiceImpl;
import com.nsu.evaluation.service.impl.CourseServiceImpl;
import com.nsu.evaluation.service.impl.ScoreServiceImpl;
import com.nsu.evaluation.utils.UUIDUtil;
import net.sf.jsqlparser.schema.Server;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping(value = "/api/evaluation/")
public class EvaluationAdminController {

    @Autowired
    private CourseServiceImpl courseServiceImpl;

    @Autowired
    private ClassServiceImpl classServiceImpl;

    @Autowired
    private ScoreServiceImpl scoreServiceImpl;

    /**
     * 跳转到课程管理界面
     * @return
     */
    @RequestMapping(value = "toCourseManage")
    public ModelAndView toCourseManage(){
        ModelAndView modelAndView = new ModelAndView("/WEB-INF/page/evaluation/course_manage.jsp");
        ServerResponse<List<Course>> serverResponse = courseServiceImpl.selectCourseInfoList();
        if(serverResponse.isSuccess()){
            List<Course> courseList = serverResponse.getData();
            modelAndView.addObject("courseList",courseList);
            return modelAndView;
        }else{
            return new ModelAndView("/WEB-INF/page/errorpage/error.jsp");
        }
    }

    /**
     * 跳转到班级管理界面
     * @return
     */
    @RequestMapping(value = "toClassManage")
    public ModelAndView toClassManage(){
        ModelAndView modelAndView = new ModelAndView("/WEB-INF/page/evaluation/class_manage.jsp");
        ServerResponse<List<Class>> serverResponse = classServiceImpl.selectClassList();
        if(serverResponse.isSuccess()){
            List<Class> courseList = serverResponse.getData();
            modelAndView.addObject("classList",courseList);
            return modelAndView;
        }else{
            return new ModelAndView("/WEB-INF/page/errorpage/error.jsp");
        }
    }

    /**
     * 跳转到成绩管理界面
     * @return
     */
    @RequestMapping(value = "toScoreManage")
    public ModelAndView toScoreManege(){
        ModelAndView modelAndView = new ModelAndView("/WEB-INF/page/evaluation/score_manage.jsp");
        ServerResponse<List<StudentScore>> serverResponse = scoreServiceImpl.getAllScoreList();
        if(serverResponse.isSuccess()){
            List<StudentScore> studentScoreList = serverResponse.getData();
            modelAndView.addObject("studentScoreList",studentScoreList);
            return modelAndView;
        }else{
            return new ModelAndView("/WEB-INF/page/errorpage/error.jsp");
        }
    }

    /**
     * 新增课程信息
     * @param course
     * @return
     */
    @RequestMapping(value = "addCourse")
    @ResponseBody
    public ServerResponse<Course> addCourse(Course course){
        String courseId = UUIDUtil.getRandomUUID();
        course.setId(courseId);
        return courseServiceImpl.addCourse(course);
    }

    /**
     * 删除课程信息
     * @param courseId
     * @return
     */
    @RequestMapping(value = "deleteCource")
    public ModelAndView deleteCource(String courseId){
        ServerResponse response = courseServiceImpl.deleteCourseByPrimaryKey(courseId);
        //不管是否删除成功都返回到管理界面
        ModelAndView modelAndView = new ModelAndView("/api/adminUsers/toUserManage");
        modelAndView.addObject("deletemsg",response.getMsg());
        return modelAndView;
    }

    /**
     * 修改课程信息
     * @param course
     * @return
     */
    @RequestMapping(value = "updateCourseByPrimaryKey")
    @ResponseBody
    public ServerResponse<Course> updateCourseByPrimaryKey(Course course){
        return courseServiceImpl.updateCourseByPrimaryKey(course);
    }

    /**
     * 根据课程名称或创建者查询课程信息
     * @param course
     * @return
     */
    @RequestMapping(value = "selectCourseInfoByExample")
    @ResponseBody
    public ServerResponse<List<Course>> selectCourseInfoByExample(Course course){
        return courseServiceImpl.selectCourseInfoByExample(course);
    }

    /**
     * 获取课程列表
     * @return
     */
    @RequestMapping(value = "selectCourseInfoList")
    @ResponseBody
    public ServerResponse<List<Course>> selectCourseInfoList(){
        return courseServiceImpl.selectCourseInfoList();
    }

    /**
     * 根据Id删除班级
     * @param classId
     * @return
     */
    @RequestMapping(value = "deleteClass")
    public ModelAndView deleteClassByPrimaryKey(String classId){
        ServerResponse response = classServiceImpl.deleteClass(classId);
        ModelAndView modelAndView = new ModelAndView("/api/evaluation/toClassManage");
        modelAndView.addObject("deleteMsg",response.getData());
        return modelAndView;
    }

    /**
     * 新增班级
     * @param clazz
     * @return
     */
    @RequestMapping(value = "addClass")
    @ResponseBody
    public ServerResponse addClass(Class clazz){
        String classId = UUIDUtil.getRandomUUID();
        clazz.setId(classId);
        return classServiceImpl.addClass(clazz);
    }

    /**
     * 新增成绩
     * @param studentScore
     * @return
     */
    @RequestMapping(value = "addScore")
    @ResponseBody
    public ServerResponse addScore(StudentScore studentScore){
        String scoreId = UUIDUtil.getRandomUUID();
        studentScore.setId(scoreId);
        return scoreServiceImpl.addStudentScore(studentScore);
    }

    /**
     * 删除成绩
     * @param scoreId
     * @return
     */
    @RequestMapping(value = "deleteScore")
    public ModelAndView deleteScoreByPrimaryKey(String scoreId){
        ServerResponse response = scoreServiceImpl.deleteStudentScore(scoreId);
        ModelAndView modelAndView = new ModelAndView("/api/evaluation/toScoreManage");
        modelAndView.addObject("deleteMsg",response.getData());
        return modelAndView;
    }
}
